<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-14
 * Time: 下午6:03
 * To change this template use File | Settings | File Templates.
 */
include 'boot.php';
session_start();

$output = array(
    'success' => true,
);

$airplanesData = json_decode($_POST['airplanes'], true);
$chessboard = new Chessboard();
foreach ($airplanesData as $data) {
    $chessboard->addAirplane(Airplane::factory($data['airplane']), $data['coordinate']);
}
Functions::log($chessboard);

$room = Room::fromId($_SESSION['room']);
if ($_SESSION['role'] == 'owner') {
    $room->setOwnerChessboard($chessboard);
    $room->save();
    if ($room->isReady()) {
        $room->noticeFighter();
    }
} elseif ($_SESSION['role'] == 'guest') {
    $room->setGuestChessboard($chessboard);
    $room->save();
    if ($room->isReady()) {
        $room->noticeFighter();
    }
} else {
    $output['success'] = false;
}

echo json_encode($output);